﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.Xml;

namespace TDLib.Rss
{
    public class RSSItem
    {
        /// <summary>Item title.</summary>
        string m_strTitle = "";

        /// <summary>Link to item.</summary>
        string m_strLink = "";

        /// <summary>Item author.</summary>
        string m_strAuthor = "";

        /// <summary>Item description.</summary>
        string m_strDescription = "";

        /// <summary>Publication timestamp.</summary>
        string m_strPublished = "";

        /// <summary>
        /// Creates an RSSItem from an element in an channel feed.
        /// </summary>
        /// <param name="element">Element in channel feed.</param>
        /// <returns>Created RSSItem or null if an error occured.</returns>
        public static RSSItem createFromElement
          (XmlElement element)
        {
            // Get item title - fail if missing
            XmlNodeList elements = element.GetElementsByTagName("title");
            if (elements.Count == 0)
                return (null);
            XmlElement ele = elements.Item(0) as XmlElement;
            string strTitle = ele.InnerText;
            strTitle = String.StringParser.removeHtml(strTitle);

            // Get item link - fail if missing
            elements = element.GetElementsByTagName("link");
            if (elements.Count == 0)
                return (null);
            ele = elements.Item(0) as XmlElement;
            string strLink = ele.InnerText;
            strLink = strLink.Replace("<![CDATA[", "");
            strLink = strLink.Replace("]]>", "");
            strLink = strLink.Trim();

            // Get optional author
            string strAuthor = "";
            elements = element.GetElementsByTagName("author");
            if (elements.Count != 0)
            {
                ele = elements.Item(0) as XmlElement;
                strAuthor = ele.InnerText.Trim();
            }

            // Get optional description
            string strDesc = "";
            elements = element.GetElementsByTagName("description");
            if (elements.Count != 0)
            {
                ele = elements.Item(0) as XmlElement;
                strDesc = ele.InnerText.Trim();
            }
            //strDesc = String.StringParser.removeHtml(strDesc);

            // Get optional publication timestamp
            string strPublished = "";
            elements = element.GetElementsByTagName("pubDate");
            if (elements.Count != 0)
            {
                ele = elements.Item(0) as XmlElement;
                strPublished = ele.InnerText;

                strPublished = strPublished.Replace("GMT+8", "+0800");

                // Remove trailing GMT offset if present
                if (strPublished.Length > 6)
                {
                    char[] chPublished = strPublished.ToCharArray();
                    char ch = chPublished[strPublished.Length - 5];
                    if ((ch == '-') || (ch == '+'))
                        strPublished = strPublished.Remove(strPublished.Length - 5, 5);
                }
                strPublished = strPublished.Trim();
            }

            // Return item
            RSSItem rssItem = new RSSItem();
            rssItem.m_strTitle = strTitle;
            rssItem.m_strLink = strLink;
            rssItem.m_strAuthor = strAuthor;
            rssItem.m_strDescription = strDesc;
            rssItem.m_strPublished = strPublished;
            return (rssItem);
        }

        /////////////
        // Properties

        /// <summary>
        /// The item's title.
        /// </summary>
        public string Title
        {
            get
            {
                return m_strTitle;
            }
        }

        /// <summary>
        /// The item's description.
        /// </summary>
        public string Description
        {
            get
            {
                return m_strDescription;
            }
        }

        /// <summary>
        /// The item's link.
        /// </summary>
        public string Link
        {
            get
            {
                return m_strLink;
            }
        }

        /// <summary>
        /// The item's author.
        /// </summary>
        public string Author
        {
            get
            {
                return m_strAuthor;
            }
        }

        /// <summary>
        /// The item's publication timestamp.
        /// </summary>
        public DateTime Published
        {
            get
            {
                DateTime tmPublished = DateTime.MinValue;
                try
                {
                    tmPublished = DateTime.Parse(m_strPublished);
                }
                catch (Exception)
                {
                    // Nothing to do
                }
                return tmPublished;
            }
        }

        /// <summary>
        /// The item's publication timestamp as a string.
        /// </summary>
        public string PublishedAsString
        {
            get
            {
                return m_strPublished;
            }
        }
    }
}
